/** 
  *@CopyRight(c)2009 company Co.Ltd. 
  *@All right reserved.
  *@module name: App.Base.SystemManage.SystemSelectTreeWindow
  *@description:
  *
  *@author: lugj
  *@version: 1.0
  *@date: 2009/06/26
  *@last date: 2009/06/26
  *@history:
    1.
    2.
  *@bugs:
    1. 
    2. 
  *
*/ 

Ext.namespace("App.Base.SystemManage");

//SystemSelectTreeWindow Class
//config: {id: ,title: ,dataUrl: ,handler: ,scope: }
App.Base.SystemManage.SystemSelectTreeWindow = function(config){
	this.config = config;
	this.MODULE_ID = App.Base.Common.func.generateGlobalUniqueId(this.config, 'SYSSELTREEWIN');

    this.setHandler = function(handler, scope){
        this.handler = handler;
        this.scope = scope;
    };

	this.setHandler(this.config.handler, this.config.scope);

	this.action = App.Base.common.param.BUTTON_ACTION_NONE;

	//tree
	this.curSelNode = this.asyncTreeNode = new Ext.tree.AsyncTreeNode({
		id: this.config.asyncTreeNode.id + this.MODULE_ID,
		text: this.config.asyncTreeNode.text,
		singleClickExpand: this.config.asyncTreeNode.singleClickExpand,
		icon: this.config.asyncTreeNode.icon,
		expandIcon: this.config.asyncTreeNode.expandIcon,
		qtip: this.config.asyncTreeNode.qtip
	});

	this.tree = new Ext.tree.TreePanel({
		id: 'tree' + this.MODULE_ID,
		autoScroll: true,
		root: this.asyncTreeNode,
		loader: new Ext.tree.TreeLoader({
			dataUrl: this.config.dataUrl,
			baseParams: {
				module: encodeURIComponent(this.MODULE_ID),
				leaf: encodeURIComponent(this.config.leaf),
				params: encodeURIComponent(this.config.params)
			}
		})
	});

	this.tree.on("beforeexpandnode", function(node){
		node.select();
		node.setIcon(node.attributes['expandIcon']);				
	});

	this.tree.on("beforecollapsenode", function(node){
		node.select();
		node.setIcon(node.attributes['icon']);				
	});

    this.contextmenu = new Ext.menu.Menu({
		id: 'ctxMenu' + this.MODULE_ID,
		items: [{
			id: 'ctxMenuExpand' + this.MODULE_ID,
			text: '展开目录节点',
			iconCls: 'x-menu-mainframe-expand'
		}, {
			id: 'ctxMenuCollapse' + this.MODULE_ID,
			text: '折叠目录节点',
			iconCls: 'x-menu-mainframe-collapse'
		}, '-', {
			id: 'ctxMenuReload' + this.MODULE_ID,
			text: '重载目录节点',
			iconCls: 'x-menu-mainframe-refresh'
		}],
		listeners: {
			click: function(menu, menuItem, e){
				this.contextmenu.hide();
			   
				var menuItemId = menuItem.getId();
				
				var node = this.tree.getSelectionModel().getSelectedNode();

				switch(menuItemId)
				{
					case 'ctxMenuExpand' + this.MODULE_ID:
						node.expand();
						break;
					case 'ctxMenuCollapse' + this.MODULE_ID:
						node.collapse();
						break;
					case 'ctxMenuReload' + this.MODULE_ID:
						node.reload();
						break;
					default:
						break;
				}
					   
			}, 
			scope: this
		}
	});
            
	this.tree.on("contextmenu", function(node, e){
		e.preventDefault();
		node.select();
		this.contextmenu.showAt(e.getXY());
	}, this);

	this.asyncTreeNode.expand();

	this.win = new Ext.Window({
		//id:  'win' + this.MODULE_ID,
		title: this.config.title,
		width: 360,
		height: 480,
		plain: true,
		maximizable: true,
		//minimizable: true,
		collapsible: true,
		constrain: true,
		modal: true,
		layout: 'fit',
		items: [this.tree],
		buttonAlign: 'right',
		buttons: [{
			text: '确 定',
			handler: function(){
				var node = this.tree.getSelectionModel().getSelectedNode();
				if (node == null)
				{
					App.Base.Common.func.MESSAGEBOX_SHOW({
						title: App.Base.common.param.MESSAGEBOX_NORMAL_TIPS,
						msg: '请选择一个树节点',
						icon: Ext.MessageBox.ERROR
					});
					return;
				}
				this.curSelNode = node;
				this.action = App.Base.common.param.BUTTON_ACTION_OK;
				this.win.close();
			},
			scope: this
		}, {
			text: '取 消',
			handler: function(){
				this.action = App.Base.common.param.BUTTON_ACTION_CANCEL;
				this.win.close();
			},
			scope: this
		}],
		
		listeners: {
			beforeclose: function(o){
				if (!(this.action == App.Base.common.param.BUTTON_ACTION_OK))
				{
					this.action = App.Base.common.param.BUTTON_ACTION_NONE;
				}
				if (this.handler)
				{
					this.handler.call(this.scope || this, this, this.curSelNode);
				}
			},
			scope: this
		}
	});

	//this.win.doLayout();

	//defined methods.
    if((typeof App.Base.SystemManage.SystemSelectTreeWindow.SIMULATE_SHARED_METHODS_FLAG) == "undefined")
    {
        App.Base.SystemManage.SystemSelectTreeWindow.SIMULATE_SHARED_METHODS_FLAG = true;   

        App.Base.SystemManage.SystemSelectTreeWindow.prototype.getMainPanel = function(){
            return this.win;  
        };
 
        var __ctor = function(){
        }();
    }
}